package com.aijk.ehm.service;

import com.aijk.base.entity.PagingRequest;
import com.aijk.commons.utils.Utils;
import com.aijk.ehm.dao.mybatis.BasUserMapper;
import com.aijk.ehm.model.BasUser;
import com.aijk.ehm.model.BasUserExample;
import com.aijk.ehm.model.qo.BasUserQo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BasUserService {
    private static Logger logger = LoggerFactory.getLogger(BasUserService.class);

    @Autowired
    private BasUserMapper mapper;

    public BasUserExample getExample(BasUserQo qo) {
        BasUserExample example = new BasUserExample();
        BasUserExample.Criteria criteria = example.createCriteria();
        if (qo != null) {
            if (Utils.isNotEmpty(qo.getSortway())) example.setOrderByClause(qo.getSortway());
            if (qo.getTitle()!=null && !qo.getTitle().equals("")) criteria.andTitleLike("%"+qo.getTitle()+"%");
            if (qo.getIdcard()!=null && !qo.getIdcard().equals("")) criteria.andIdcardEqualTo(qo.getIdcard());
            if (qo.getCardtype()!=null && qo.getCardtype() != 0) criteria.andCardtypeEqualTo(qo.getCardtype());
            if (qo.getId()!=null && Utils.toInteger(qo.getId()) != 0) criteria.andIdEqualTo(qo.getId());
            if (qo.getInIdCards()!=null && qo.getInIdCards().size()>0) criteria.andIdcardIn(qo.getInIdCards());
            if (qo.getInIds()!=null && qo.getInIds().size()>0) criteria.andIdIn(qo.getInIds());
        }
        return example;
    }

    public int count(BasUserQo qo) {
        return mapper.countByExample(getExample(qo));
    }

    public int save(BasUser bean) {
        return mapper.insertSelective(bean);
    }

    public int delete(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    public int update(BasUser bean) {
        return mapper.updateByPrimaryKeySelective(bean);
    }

    public BasUser findOne(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<BasUser> find(BasUserQo qo) {
        return mapper.selectByExample(getExample(qo));
    }

    public PageInfo<BasUser> page(PagingRequest pagingRequest, BasUserQo qo) {
        PageHelper.startPage(pagingRequest.getPageNum() , pagingRequest.getPageSize());
        return new PageInfo<BasUser>(mapper.selectByExample(getExample(qo)));
    }
}